package com.bytedance.common.b;

import android.app.ActivityManager;
import android.os.Build;
import android.webkit.WebSettings;
import android.webkit.WebView;

import java.lang.reflect.Method;

public final class WebViewPauseResumeCompat {
    private static Method method_onPause;
    private static Method method_onResume;
    private static boolean reflectMethod = false;

    public static int getLargeMemoryClass(ActivityManager activityManager) {
        if (Build.VERSION.SDK_INT >= 11) {
            try {
                return activityManager.getLargeMemoryClass();
            } catch (Throwable throwable) {
            }
        }
        return -1;
    }

    private static void reflectMethods() {
        if (!reflectMethod) {
            reflectMethod = true;
            try {
                method_onPause = WebView.class.getMethod("onPause");
                method_onResume = WebView.class.getMethod("onResume");
            } catch (Exception exception) {
            }
        }
    }

    public static void setDisplayZoomControls(WebSettings webSettings) {
        if (Build.VERSION.SDK_INT >= 11) {
            webSettings.setDisplayZoomControls(false);
        }
    }

    public static void onPause(WebView webView) {
        if (webView != null) {
            if (Build.VERSION.SDK_INT >= 11) {
                webView.onPause();
                return;
            }

            reflectMethods();
            if (method_onPause != null) {
                try {
                    method_onPause.invoke(webView);
                } catch (Exception exception) {
                }
            }
        }
    }

    public static void onResume(WebView webView) {
        if (webView != null) {
            if (Build.VERSION.SDK_INT >= 11) {
                webView.onResume();
                return;
            }

            reflectMethods();
            if (method_onResume != null) {
                try {
                    method_onResume.invoke(webView);
                } catch (Exception exception) {
                }
            }
        }
    }
}

